<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" @close="onClose">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
      label-width="80px">
      <el-form-item label="banner链接" prop="imgUrl">
        <!-- <el-input v-model="dataForm.imgUrl" placeholder="banner链接"></el-input> -->
        <singleUpload v-model="dataForm.imgUrl"></singleUpload>
      </el-form-item>
      <el-form-item label="跳转链接" prop="hrefurl">
        <el-input v-model="dataForm.hrefurl" placeholder="跳转链接
"></el-input>
      </el-form-item>
      <el-form-item label="广告类型" prop="type">
        <!-- <el-input v-model="dataForm.type" placeholder="跳转类型"></el-input> -->
        <el-select v-model="dataForm.type" placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click=" onClose()">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import singleUpload from '../common/upload/singleUpload.vue';
export default {
  components: {
    singleUpload
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        imgUrl: '',
        hrefurl: '',
        type: ''
      },
      dataRule: {
        imgUrl: [
          { required: true, message: 'banner链接不能为空', trigger: 'blur' }
        ],
        hrefurl: [
          {
            required: true, message: '跳转链接不能为空', trigger: 'blur'
          }
        ],
        type: [
          { required: true, message: '广告类型不能为空', trigger: 'blur' }
        ]
      },
      options: [{
        value: 1,
        label: '首页展示'
      }, {
        value: 2,
        label: '分类页展示'
      }],
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/product/banner/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.dataForm.imgUrl = data.data.imgUrl
              this.dataForm.hrefurl = data.data.hrefurl
              this.dataForm.type = data.data.type
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/product/banner/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'imgUrl': this.dataForm.imgUrl,
              'hrefurl': this.dataForm.hrefurl,
              'type': this.dataForm.type
            })
          }).then(({ data }) => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    onClose() {
      this.visible = false
      this.dataForm.imgUrl = ''
    },
  }
}
</script>
